﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win;
using Infragistics.Documents.Excel;
using CrystalDecisions.CrystalReports.Engine;

namespace POS_NEW
{
    public partial class frmChiPhi : Form
    {
        public frmChiPhi()
        {
            InitializeComponent();
        }
        int ID_ChiPhi_Chitiet = -1;
        object TuNgay,DenNgay;
        #region "Load"
        private void LoadcboChiPhi()
        {
            cboChiPhi.DataSource = null;
            DataTable dt = AccessDataLayer.ExecuteDT("spChiPhi_Select", CommandType.StoredProcedure);
            DataRow dr = dt.NewRow();
            dr["Mã chi phí"] = -1;
            dr["Chi phí"] = " ----Chọn chi phí----";
            dt.Rows.Add(dr);
            cboChiPhi.DataSource = dt; 
            cboChiPhi.ValueMember = "Mã chi phí";
            cboChiPhi.DisplayMember = "Chi phí";
            cboChiPhi.SortStyle = ValueListSortStyle.Ascending;
            cboChiPhi.AutoCompleteMode = Infragistics.Win.AutoCompleteMode.SuggestAppend;
            if (cboChiPhi.Items.Count > 0)
                cboChiPhi.SelectedIndex = 0;
        }
      
        private void LoadgridChiPhi_Chitiet(string Ten_NhanVien,object TuNgay,object DenNgay)
        {
            gridChiPhi_Chitiet.DataSource = AccessDataLayer.ExecuteDT("spChiPhi_Chitiet_Select", CommandType.StoredProcedure
                , "@ID_ChiNhanh", myProgress.ID_ChiNhanh
                , "@Ten_NhanVien", Ten_NhanVien
                , "@TuNgay", TuNgay
                , "@DenNgay", DenNgay
                );
            gridChiPhi_Chitiet.DisplayLayout.Bands[0].Override.RowAppearance.BackColor = Color.Moccasin;
            gridChiPhi_Chitiet.DisplayLayout.Bands[0].Columns["Mã chi phí chi tiết"].Hidden = true;
            gridChiPhi_Chitiet.DisplayLayout.Bands[0].Columns["Mã chi phí"].Hidden = true;
            gridChiPhi_Chitiet.DisplayLayout.Bands[0].Columns["Chi nhánh"].Hidden = true;
            gridChiPhi_Chitiet.DisplayLayout.Bands[0].Columns["Ngày"].Format = "dd/MM/yyyy";
            gridChiPhi_Chitiet.DisplayLayout.Bands[0].Columns["Số tiền"].Format = "0,000đ";
            gridChiPhi_Chitiet.DisplayLayout.Bands[0].Columns["Số tiền"].CellAppearance.TextHAlign = HAlign.Right;

            gridChiPhi_Chitiet.DisplayLayout.Bands[0].Columns["Chi phí"].CellMultiLine = DefaultableBoolean.True;
            gridChiPhi_Chitiet.DisplayLayout.Bands[0].Columns["Mô tả"].CellMultiLine = DefaultableBoolean.True;

            

            gridChiPhi_Chitiet.DisplayLayout.Override.GroupByRowAppearance.FontData.Bold = DefaultableBoolean.True;
            gridChiPhi_Chitiet.DisplayLayout.Bands[0].Override.CellClickAction = CellClickAction.RowSelect;
            gridChiPhi_Chitiet.DisplayLayout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.ResizeAllColumns;
            gridChiPhi_Chitiet.DisplayLayout.PerformAutoResizeColumns(true, Infragistics.Win.UltraWinGrid.PerformAutoSizeType.VisibleRows);

            gridChiPhi_Chitiet.DisplayLayout.Bands[0].Columns["Chi phí"].Width = 70;
            gridChiPhi_Chitiet.DisplayLayout.Bands[0].Columns["Mô tả"].Width = 80;
            gridChiPhi_Chitiet.DisplayLayout.Bands[0].SortedColumns.Add("Ngày",true, true);
            gridChiPhi_Chitiet.DisplayLayout.ViewStyleBand = ViewStyleBand.OutlookGroupBy;
            gridChiPhi_Chitiet.Rows.ExpandAll(true);
            if (gridChiPhi_Chitiet.Rows.Count == 0)
            {
                btnIn.Enabled = false;
                btnXuatExcel.Enabled = false;
            }
            else
            {
                btnIn.Enabled = true;
                btnXuatExcel.Enabled = true;
            }
        }
        #endregion
        #region "Thêm"
        private int ThemChiPhi_Chitiet(DateTime NgayChi,string ID_ChiPhi,float SoTien,string GhiChu,string ID_ChiNhanh,int ID_NhanVien)
        {
            object[] ob={"@NgayChi",NgayChi,"@ID_ChiPhi",ID_ChiPhi,"SoTien",SoTien,"@MoTa",GhiChu,"@ID_ChiNhanh",ID_ChiNhanh,"@ID_NhanVien",ID_NhanVien};
            return AccessDataLayer.ExecuteNonQuery("spChiPhi_Chitiet_Insert",CommandType.StoredProcedure,ob);
        }
        #endregion
        #region "Lưu"
        private int LuuChiPhi_Chitiet(string ID_Chitiet,DateTime NgayChi, string ID_ChiPhi, float SoTien, string GhiChu,string ID_ChiNhanh,int ID_NhanVien)
        {
            object[] ob = {"@ID_ChiPhi_Chitiet",ID_Chitiet, "@NgayChi", NgayChi
                              , "@ID_ChiPhi", ID_ChiPhi, "SoTien", SoTien, "@MoTa", GhiChu,"@ID_ChiNhanh",ID_ChiNhanh,"@ID_NhanVien",ID_NhanVien };
            return AccessDataLayer.ExecuteNonQuery("spChiPhi_Chitiet_Update", CommandType.StoredProcedure, ob);
        }
        #endregion
        #region "Xóa"
        private int XoaChiPhi_Chitiet(int ID_Chitiet)
        {
            return AccessDataLayer.ExecuteNonQuery("spChiPhi_Chitiet_Delete", CommandType.StoredProcedure, "@ID_ChiPhi_Chitiet", ID_Chitiet);
        }
        #endregion
        #region "Xuất Excel"
        #endregion
        private void frmChiPhi_Load(object sender, EventArgs e)
        {
            this.dtNgayChi.FormatProvider = new System.Globalization.CultureInfo("vi-VN");
            this.dtTuNgay.FormatProvider = new System.Globalization.CultureInfo("vi-VN");
            this.dtDenNgay.FormatProvider = new System.Globalization.CultureInfo("vi-VN");
            LoadcboChiPhi();
            LoadgridChiPhi_Chitiet("",DateTime.Today,DateTime.Today);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gridChiPhi_Chitiet_BeforeCellActivate(object sender, CancelableCellEventArgs e)
        {
            e.Cell.Activation = Activation.NoEdit;
        }

      

       

        private void btnChitiet_Them_Click(object sender, EventArgs e)
        {
            dtNgayChi.Enabled = true;
            cboChiPhi.Enabled = true;
            cboChiPhi.SelectedIndex = 0;
            txtSoTien.Enabled = true;
            txtSoTien.ResetValue();
            txtChitiet_GhiChu.Enabled = true;
            txtChitiet_GhiChu.ResetText();
            btnChitiet_Luu.Enabled = true;
            ID_ChiPhi_Chitiet = -1;
            dtNgayChi.Focus();
        }

        private void btnChitiet_Luu_Click(object sender, EventArgs e)
        {
            if (cboChiPhi.SelectedIndex == 0)
                return;
            if (txtSoTien.Value == 0)
                return;
            if (ID_ChiPhi_Chitiet == -1)//thêm
            {
                int kq = -1;
                try
                {
                    kq = ThemChiPhi_Chitiet(dtNgayChi.DateTime, cboChiPhi.Value.ToString(), float.Parse(txtSoTien.Value.ToString()), txtChitiet_GhiChu.Text,myProgress.ID_ChiNhanh,myProgress.ID_NhanVien);
                }
                catch (Exception ex)
                {
                    using (ErrorScreen ers = new ErrorScreen(ex.Message))
                        ers.ShowDialog();
                    return;
                }
                if (kq > 0)
                    using (ErrorScreen ers = new ErrorScreen("Lưu thành công !", "Thông báo", 3))
                    {
                        ers.ShowDialog();
                        TuNgay = dtNgayChi.Value;
                        DenNgay = dtNgayChi.Value;
                        LoadgridChiPhi_Chitiet("",TuNgay,DenNgay);
                        btnChitiet_Them.Focus();
                    }
                else
                {
                    using (ErrorScreen ers = new ErrorScreen("Có lỗi xảy ra !", "Thông báo", 3))
                        ers.ShowDialog();
                    return;
                }
            }
            else   //sửa
            {
                int kq = -1;
                try
                {
                    kq = LuuChiPhi_Chitiet(ID_ChiPhi_Chitiet.ToString(),dtNgayChi.DateTime, cboChiPhi.Value.ToString(), float.Parse(txtSoTien.Value.ToString()), txtChitiet_GhiChu.Text,myProgress.ID_ChiNhanh,myProgress.ID_NhanVien);
                }
                catch (Exception ex)
                {
                    using (ErrorScreen ers = new ErrorScreen(ex.Message))
                        ers.ShowDialog();
                    return;
                }
                if (kq > 0)
                    using (ErrorScreen ers = new ErrorScreen("Lưu thành công !", "Thông báo", 3))
                    {
                        ers.ShowDialog();
                        TuNgay = dtNgayChi.Value;
                        DenNgay = dtNgayChi.Value;
                        LoadgridChiPhi_Chitiet("", TuNgay, DenNgay);
                    }
                else
                {
                    using (ErrorScreen ers = new ErrorScreen("Có lỗi xảy ra !", "Thông báo", 3))
                        ers.ShowDialog();
                    return;
                }
            }
            dtNgayChi.Enabled = false;
            cboChiPhi.Enabled = false;
            txtSoTien.Enabled = false;
            txtChitiet_GhiChu.Enabled = false;
            btnChitiet_Luu.Enabled = false;

        }

        private void txtChiPhi_GhiChu_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dtNgayChi_Enter(object sender, EventArgs e)
        {
            dtNgayChi.SelectAll();
        }

        private void cboChiPhi_Enter(object sender, EventArgs e)
        {

            cboChiPhi.SelectAll();
        }

        private void txtSoTien_Enter(object sender, EventArgs e)
        {
            txtSoTien.SelectAll();
        }

        private void txtChitiet_GhiChu_Enter(object sender, EventArgs e)
        {
            txtChitiet_GhiChu.SelectAll();
        }

     

        private void dtNgayChi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cboChiPhi.Focus();
            }
        }

        private void cboChiPhi_KeyDown(object sender, KeyEventArgs e)
        {
            if (cboChiPhi.SelectedIndex == 0)
                return;
            if (e.KeyCode == Keys.Enter)
            {
                txtSoTien.Focus();
            }
        }
        private void txtSoTien_KeyDown(object sender, KeyEventArgs e)
        {
            if (txtSoTien.Value == 0)
                return;
            if (e.KeyCode == Keys.Enter)
            {
                txtChitiet_GhiChu.Focus();
            }
        }
        private void txtChitiet_GhiChu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChitiet_Luu.Focus();
            }
        }

        private void dtTuNgay_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                dtDenNgay.Focus();
            }
        }

        private void dtDenNgay_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtNhanVien.Focus();
            }
        }

        private void dtTuNgay_Enter(object sender, EventArgs e)
        {
            dtTuNgay.SelectAll();
        }

        private void dtDenNgay_Enter(object sender, EventArgs e)
        {
            dtDenNgay.SelectAll();
        }

        private void txtNhanVien_Enter(object sender, EventArgs e)
        {
            txtNhanVien.SelectAll();
        }


       

        private void gridChiPhi_Chitiet_ClickCell(object sender, ClickCellEventArgs e)
        {
            if (gridChiPhi_Chitiet.Rows.Count > 0)
                if (gridChiPhi_Chitiet.ActiveRow != null )
                    if(gridChiPhi_Chitiet.ActiveRow.Cells!=null)
                {
                    dtNgayChi.Enabled = true;
                    cboChiPhi.Enabled = true;
                    txtSoTien.Enabled = true;
                    txtChitiet_GhiChu.Enabled = true;
                    btnChitiet_Luu.Enabled = true;
                    //
                    dtNgayChi.Value = gridChiPhi_Chitiet.ActiveRow.Cells["Ngày"].Value;
                    cboChiPhi.Value = gridChiPhi_Chitiet.ActiveRow.Cells["Mã chi phí"].Value;
                    txtSoTien.Value = decimal.Parse(gridChiPhi_Chitiet.ActiveRow.Cells["Số tiền"].Value.ToString());
                    txtChitiet_GhiChu.Text = gridChiPhi_Chitiet.ActiveRow.Cells["Mô tả"].Text;
                    ID_ChiPhi_Chitiet = int.Parse(gridChiPhi_Chitiet.ActiveRow.Cells["Mã chi phí chi tiết"].Text.ToString());
                }
        }

        private void gridChiPhi_Chitiet_AfterSelectChange(object sender, AfterSelectChangeEventArgs e)
        {
            gridChiPhi_Chitiet_ClickCell(null, null);
        }

        private void btnChiPhi_Click(object sender, EventArgs e)
        {
            using (frmChiPhi_ChinhSua frm = new frmChiPhi_ChinhSua())
            {
                if (frm.ShowDialog() == DialogResult.Cancel)
                {
                    LoadcboChiPhi();
                    dtTuNgay.DateTime=dtDenNgay.DateTime = DateTime.Today;
                    TuNgay = dtTuNgay.Value;
                    DenNgay = dtDenNgay.Value;
                    LoadgridChiPhi_Chitiet("", TuNgay, DenNgay);
                }
            }
        }

        private void btnChitiet_Xoa_Click(object sender, EventArgs e)
        {
            if (gridChiPhi_Chitiet.Rows.Count > 0)
                if (gridChiPhi_Chitiet.ActiveRow != null)
                {
                    if (ID_ChiPhi_Chitiet == -1)
                        return;
                    using (ConfirmScreen cfs = new ConfirmScreen("Bạn có muốn xóa ?"))
                    {
                        if (cfs.ShowDialog() == DialogResult.OK)
                        {
                            try
                            {
                                if (this.XoaChiPhi_Chitiet(ID_ChiPhi_Chitiet) > 0)
                                {
                                    using (ErrorScreen er = new ErrorScreen("Đã xóa ", "Thông báo", 3))
                                    {
                                        er.ShowDialog();
                                        TuNgay = dtNgayChi.Value;
                                        DenNgay = dtNgayChi.Value;
                                        LoadgridChiPhi_Chitiet("", TuNgay, DenNgay);
                                    }
                                }
                                else
                                {
                                    using (ErrorScreen ers = new ErrorScreen("Có lỗi xảy ra !", "Thông báo", 3))
                                        ers.ShowDialog();
                                    return;
                                }
                            }
                            catch (Exception ex)
                            {
                                using (ErrorScreen ee = new ErrorScreen(ex.Message))
                                {
                                    ee.ShowDialog();
                                }
                            }
                        }
                        else
                        {
                            gridChiPhi_Chitiet.DisplayLayout.Bands[0].Override.AllowDelete = DefaultableBoolean.False;
                        }
                    }
                }
        }

        private void btnXuatExcel_Click(object sender, EventArgs e)
        {
            if (gridChiPhi_Chitiet.Rows.Count > 0)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Excel file|.xls";
                sfd.FileName = "ChiPhi_" + DateTime.Now.Year + "" + DateTime.Now.Month + DateTime.Now.Day;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        Workbook wf = new Workbook();
                        ultragridXuatExcel.Export(gridChiPhi_Chitiet, wf, 5, 1);
                        wf.Save(sfd.FileName);

                        System.Diagnostics.Process.Start(sfd.FileName);
                    }
                    catch (Exception ex)
                    {
                        using (ErrorScreen ers = new ErrorScreen(ex.Message))
                            ers.ShowDialog();
                    }
                }
            }
        }

        private void btnXemTatCa_Click(object sender, EventArgs e)
        {
            dtTuNgay.DateTime = dtDenNgay.DateTime = DateTime.Today;
            TuNgay =DenNgay= null;
            LoadgridChiPhi_Chitiet("", TuNgay, DenNgay);
        }
        private void frmChiPhi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F3)
                btnChitiet_Them_Click(null, null);
            else
                if (e.KeyCode == Keys.F4)
                {
                    if(btnChitiet_Luu.Enabled)
                    btnChitiet_Luu_Click(null, null);
                }
                else
                    if (e.KeyCode == Keys.Delete)
                        btnChitiet_Xoa_Click(null, null);
                    else
                        if (e.KeyCode == Keys.F1)
                            btnChiPhi_Click(null, null);
                        else if (e.KeyCode == Keys.F2)
                            btnXuatExcel_Click(null, null);
                        else if (e.KeyCode == Keys.F9)
                            btnIn_Click(null, null);
                        else
                            if (e.KeyCode == Keys.F8)
                                dtTuNgay.Focus();
                            else
                                if (e.KeyCode == Keys.F5)
                                    btnXemTatCa_Click(null, null);
                                else
                                    if (e.KeyCode == Keys.Escape)
                                        btnThoat_Click(null, null);
        }

        private void dtTuNgay_ValueChanged(object sender, EventArgs e)
        {
            TuNgay = dtTuNgay.Value;
            DenNgay = dtDenNgay.Value;
            LoadgridChiPhi_Chitiet(txtNhanVien.Text, TuNgay, DenNgay);
        }

        private void dtDenNgay_ValueChanged(object sender, EventArgs e)
        {
            TuNgay = dtTuNgay.Value;
            DenNgay = dtDenNgay.Value;
            LoadgridChiPhi_Chitiet(txtNhanVien.Text, TuNgay, DenNgay);
        }

        private void txtNhanVien_TextChanged(object sender, EventArgs e)
        {
            LoadgridChiPhi_Chitiet(txtNhanVien.Text, TuNgay, DenNgay);
        }


        private void ultragridXuatExcel_BeginExport(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.BeginExportEventArgs e)
        {
            e.CurrentWorksheet.Rows[1].Cells[2].Value = "Thông tin Chi phí";

            e.CurrentWorksheet.Rows[1].Cells[2].CellFormat.Font.Bold = ExcelDefaultableBoolean.True;
            e.CurrentWorksheet.Rows[1].Cells[2].CellFormat.Font.Name = "Tahoma";
            e.CurrentWorksheet.Rows[1].Cells[2].CellFormat.Font.Height = 300;
            e.CurrentWorksheet.Rows[1].Cells[2].Worksheet.MergedCellsRegions.Add(1, 2, 1, 5);
            e.CurrentWorksheet.Rows[2].Cells[6].Value = "Ngày xuất: " + DateTime.Now.ToString("dd/MM/yyyy");
            e.CurrentWorksheet.Rows[2].Cells[6].CellFormat.Font.Name = "Tahoma";
            e.CurrentWorksheet.Rows[2].Cells[6].CellFormat.Font.Italic = ExcelDefaultableBoolean.True;
            e.CurrentWorksheet.Rows[3].Cells[1].CellFormat.Font.Bold = ExcelDefaultableBoolean.True;
            e.CurrentWorksheet.Rows[3].Cells[1].CellFormat.Font.Name = "Tahoma";
            e.CurrentWorksheet.Rows[3].Cells[1].CellFormat.Font.Height = 230;
            e.CurrentWorksheet.Rows[1].Cells[3].CellFormat.Alignment = HorizontalCellAlignment.Center;
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            if (btnIn.Enabled == false)
                return;
            //POS_NEW.report.rptChiPhi rpt = new POS_NEW.report.rptChiPhi();
            ReportDocument rpt = new ReportDocument();
            rpt.Load(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("bin")) + @"\report\rptChiPhi.rpt");
            rpt.SetDataSource ( gridChiPhi_Chitiet.DataSource);
            myProgress.SubReportHeader(rpt);
            using (frmDisplayReport frm = new frmDisplayReport(rpt))
            {
                if (frm.ShowDialog() == DialogResult.Cancel)
                    rpt.Dispose();
            }
            //for (int i = 1; i <= 1; i++)
            //    rpt.PrintToPrinter(0, true, 1, 100);

            //rpt.Dispose();
        }

      
    }
}